package fr.afcepf.iziticket.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import fr.afcepf.iziticket.entityviews.ReservationVue;
import fr.afcepf.iziticket.services.PanierService;

@ManagedBean(name="mbPanier")
@SessionScoped
public class PanierManagedBean {

	private PanierService service = PanierService.getService();
	
	@ManagedProperty(value="#{mbLogin}")
	private LoginManagedBean mbLogin;
	@ManagedProperty(value="#{mbConfirmation}")
	private ConfirmationCommandeManagedBean mbConfirmationCommande;
	private int compteurPanier;
	private List<ReservationVue> reservationsVues = new ArrayList<ReservationVue>();
	
	public ConfirmationCommandeManagedBean getMbConfirmationCommande() {
		return mbConfirmationCommande;
	}
	public void setMbConfirmationCommande(
			ConfirmationCommandeManagedBean mbConfirmationCommande) {
		this.mbConfirmationCommande = mbConfirmationCommande;
	}
	public int getCompteurPanier() {
		return compteurPanier;
	}
	public void setCompteurPanier(int compteurPanier) {
		this.compteurPanier = compteurPanier;
	}
	public LoginManagedBean getMbLogin() {
		return mbLogin;
	}
	public void setMbLogin(LoginManagedBean mbLogin) {
		this.mbLogin = mbLogin;
	}
	public List<ReservationVue> getReservationsVues() {
		return reservationsVues;
	}
	public void setReservationsVues(List<ReservationVue> reservationsVues) {
		this.reservationsVues = reservationsVues;
	}
	
	public float getPrixTotal(){
		float prixTotal = 0;
		
		for (ReservationVue resv : reservationsVues) {
			prixTotal+=resv.getPrixReservation();
		}
		
		return prixTotal;
	}
	
	public int getSize(){
		return reservationsVues.size();
	}
	
	public void supprimer(ActionEvent ae){
		
		int idReservation = Integer.parseInt(ae.getComponent().getAttributes().get("resId").toString());
		
		ReservationVue vueASupp=null;
		
		for (ReservationVue resV : reservationsVues) {
			if(resV.getIdPanier()==idReservation){
				vueASupp = resV;
			}
		}

		reservationsVues.remove(vueASupp);
		
	}
	
	public String valider(){
		
		if(reservationsVues.size()!=0){
			
			if(service.valider(mbLogin.getUtilisateur(), reservationsVues)){
				
				reservationsVues.clear();
		
				mbConfirmationCommande.init();
				
				FacesContext facesContext = FacesContext.getCurrentInstance();
				NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
			    myNav.handleNavigation(facesContext, null, "/confirmationCommande.xhtml");

			}
			else{
				
				FacesContext facesContext = FacesContext.getCurrentInstance();
				NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
			    myNav.handleNavigation(facesContext, null, "/connexion.xhtml");
				
			}
		}
		
		return "";	
	}
	
	public String ajQtte(ActionEvent ae){
		
		int idRes = Integer.parseInt(ae.getComponent().getAttributes().get("resId").toString());
		
		for (ReservationVue res : reservationsVues) {
			if(res.getIdPanier()==idRes){
					res.getReservation().setQuantite(res.getReservation().getQuantite()+1);
			}
		}
		return "";
	}
	
	public String supQtte(ActionEvent ae){
		
		int idRes = Integer.parseInt(ae.getComponent().getAttributes().get("resId").toString());
		
		ReservationVue resv = null;
		
		for (ReservationVue res : reservationsVues) {
			if(res.getIdPanier()==idRes){
				if(res.getReservation().getQuantite()>1){
					res.getReservation().setQuantite(res.getReservation().getQuantite()-1);
				}
				else{
					resv=res;
				}
			}
		}
		
		if(resv!=null){
			reservationsVues.remove(resv);
		}
		
		return "";
	}
	
	public void ajouter(int quantite, int idSeance){

		boolean dejaExistant=false;
		for (ReservationVue resv : reservationsVues) {
			if(resv.getReservation().getSeance().getIdSeance()==idSeance){
				resv.getReservation().setQuantite(resv.getReservation().getQuantite()+quantite);
				dejaExistant=true;
			}
		}
		if(!dejaExistant){
			
			ReservationVue reservVue = service.getSeance(idSeance, quantite);

			reservVue.setIdPanier(compteurPanier++);
			
			reservationsVues.add(reservVue);
		}
	}
	
}
